home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 401-425 / disk_417 / mostcurrent / mostcurrent.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  227 lines

  1. /* MostCurrent.c
  2.  *
  3.  * Programmed by: Peter A. Phelps 11/9/90
  4.  *
  5.  * See MostCurrent.doc for further info.
  6.  *
  7.  */
  8.  
  9. #include    <ctype.h>
  10. #include    <stdio.h>
  11. #include    <functions.h>
  12. #include    <exec/types.h>
  13. #include    <exec/memory.h>
  14. #include    <intuition/intuition.h>
  15.  
  16. void    Upcase();
  17. void    _abort();
  18.  
  19. #define    SIZE        10000
  20. #define    BYTE_CNT    2048
  21. #define    ESC            27
  22.  
  23. char    *EntryNames[SIZE];
  24. int        HighestDisk[SIZE];
  25.  
  26. struct    NameData
  27. {
  28.     char    Bytes[BYTE_CNT];
  29. } Element;
  30.  
  31. struct    IntuitionBase    *IntuitionBase;
  32.  
  33. struct    NameData    *ElementPointer = ∈
  34.  
  35. struct    Remember    *RememberKey = (struct Remember *)NULL;
  36.  
  37. main()
  38. {
  39.     FILE    *Index, *NewIndex, *Names;
  40.     union
  41.     {
  42.         struct
  43.         {
  44.             ULONG    BitMask;
  45.             SHORT    FFDisk;
  46.             ULONG    AbsAdr;
  47.             SHORT    ContentsLength;
  48.             UBYTE    Space___The_Final_Frontier;
  49.         } Entry;
  50.         struct
  51.         {
  52.             UBYTE    Byte[13];
  53.         } ByteView;
  54.     } Record;
  55.     char    InChar, ThisEntry[20], Null = '\0';
  56.     int        i, NextEntry = 1, Lowest, Highest, NextByte = 0;
  57.     SHORT    ThisFish = 0;
  58.  
  59.     if((IntuitionBase = (struct IntuitionBase *)
  60.         OpenLibrary("intuition.library",LIBRARY_VERSION)) ==
  61.         (struct IntuitionBase *)NULL)
  62.     {
  63.         printf("Need intuition.library Ver >= %d.\n",LIBRARY_VERSION);
  64.         exit(FALSE);
  65.     }
  66.  
  67.     if((Index = fopen("index.tmp","r")) == (struct FILE *)NULL)
  68.     {
  69.         printf("index.tmp not found!\nSee MostCurrent.doc\n");
  70.         exit(FALSE);
  71.     }
  72.  
  73.     if((Names = fopen("Names","r")) == (struct FILE *)NULL)
  74.     {
  75.         printf("Names file not found!\nSee MostCurrent.doc\n");
  76.         fclose(Index);
  77.         exit(FALSE);
  78.     }
  79.  
  80.     if((NewIndex = fopen("index","r")) != (struct FILE *)NULL)
  81.     {
  82.         printf("index already exists!\nSee MostCurrent.doc\n");
  83.         fclose(NewIndex);
  84.         fclose(Index);
  85.         fclose(Names);
  86.         exit(FALSE);
  87.     }
  88.  
  89.     printf("Reading Names and index.tmp files\n");
  90.     printf("to determine the MostCurrent Button data\n\n");
  91.  
  92.     EntryNames[0] = &Null;
  93.     HighestDisk[0] = 0; /* In case there are any Null Names */
  94.  
  95.     while((InChar = getc(Index)) != EOF)
  96.     {
  97.         for(i=0;i<13;i++)
  98.         {
  99.             Record.ByteView.Byte[i] = InChar;
  100.             InChar = getc(Index);
  101.         }
  102.         for(i=0;i<20;i++)
  103.             ThisEntry[i] = getc(Names);
  104.         for(i=0;i<19 && ThisEntry[i] != ' ';i++);
  105.         ThisEntry[i] = '\0';
  106.         Upcase(ThisEntry);
  107.         Lowest = 0;
  108.         Highest = NextEntry;
  109.         while(Lowest+1 < Highest)
  110.             if(!strcmp(EntryNames[(Highest-Lowest)/2+Lowest],ThisEntry))
  111.                 Lowest = Highest = (Highest-Lowest)/2+Lowest;
  112.             else
  113.                 if(strcmp(EntryNames[(Highest-Lowest)/2+Lowest],ThisEntry)>0)
  114.                     Highest = (Highest-Lowest)/2+Lowest;
  115.                 else
  116.                     Lowest = (Highest-Lowest)/2+Lowest;
  117.         if(strcmp(EntryNames[Lowest],ThisEntry))
  118.         {
  119.             Lowest++;
  120.             for(i=NextEntry; i>Lowest; i--)
  121.             {
  122.                 EntryNames[i] = EntryNames[i-1];
  123.                 HighestDisk[i] = HighestDisk[i-1];
  124.             }
  125.             NextEntry++;
  126.             if(NextByte + strlen(ThisEntry) + 1 > BYTE_CNT)
  127.             {
  128.                 if((ElementPointer = (struct NameData *)
  129.                 AllocRemember(&RememberKey,sizeof(struct NameData),
  130.                     MEMF_FAST)) == (struct NameData *)NULL)
  131.                 {
  132.                     FreeRemember(&RememberKey,TRUE);
  133.                     fclose(Index);
  134.                     CloseLibrary(IntuitionBase);
  135.                     printf("Out of Memory!\n");
  136.                     printf("Please free some memory,");
  137.                     printf("and run this program again.\n");
  138.                     exit(FALSE);
  139.                 }
  140.                 NextByte = 0;
  141.             }
  142.             EntryNames[Lowest] =
  143.                 &ElementPointer->Bytes[NextByte];
  144.             i = 0;
  145.             while(ThisEntry[i])
  146.                 ElementPointer->Bytes[NextByte++] =
  147.                     ThisEntry[i++];
  148.             ElementPointer->Bytes[NextByte++] = '\0';
  149.             HighestDisk[Lowest] = 0;
  150.         }
  151.         if(Record.Entry.FFDisk > HighestDisk[Lowest])
  152.             HighestDisk[Lowest] = Record.Entry.FFDisk;
  153.         if(Record.Entry.FFDisk != ThisFish)
  154.             printf("%c[AFish #%d\n",ESC,Record.Entry.FFDisk);
  155.         ThisFish = Record.Entry.FFDisk;
  156.     }
  157.  
  158.     fseek(Index,0,0);
  159.     fseek(Names,0,0);
  160.     if((NewIndex = fopen("index","x")) == (struct FILE *)NULL)
  161.     {
  162.         printf("Problem opening index.\n");
  163.         printf("This shouldn't happen!\n");
  164.         printf("Is your disk write protected?\n");
  165.         FreeRemember(&RememberKey,TRUE);
  166.         exit(FALSE);
  167.     }
  168.  
  169.     printf("%c[3A%c[JWriting MostCurrent Info\n\n",ESC,ESC);
  170.  
  171.     while((InChar = getc(Index)) != EOF)
  172.     {
  173.         for(i=0;i<13;i++)
  174.         {
  175.             Record.ByteView.Byte[i] = InChar;
  176.             InChar = getc(Index);
  177.         }
  178.         for(i=0;i<20;i++)
  179.             ThisEntry[i] = getc(Names);
  180.         for(i=0;i<19 && ThisEntry[i] != ' '; i++);
  181.         ThisEntry[i] = '\0';
  182.         Upcase(ThisEntry);
  183.         Lowest = 0;
  184.         Highest = NextEntry;
  185.         while(Lowest+1 < Highest)
  186.             if(!strcmp(EntryNames[(Highest-Lowest)/2+Lowest],ThisEntry))
  187.                 Lowest = Highest = (Highest-Lowest)/2+Lowest;
  188.             else
  189.                 if(strcmp(EntryNames[(Highest-Lowest)/2+Lowest],ThisEntry)>0)
  190.                     Highest = (Highest-Lowest)/2+Lowest;
  191.                 else
  192.                     Lowest = (Highest-Lowest)/2+Lowest;
  193.  
  194.         Record.Entry.BitMask &= ~0x20000000;
  195.         if(Record.Entry.FFDisk == HighestDisk[Lowest])
  196.             Record.Entry.BitMask |= 0x20000000;
  197.         for(i=0; i<13;i++)
  198.             putc(Record.ByteView.Byte[i],NewIndex);
  199.         putc(0,NewIndex);
  200.         if(Record.Entry.FFDisk != ThisFish)
  201.             printf("%c[AFish #%d\n",ESC,Record.Entry.FFDisk);
  202.         ThisFish = Record.Entry.FFDisk;
  203.     }
  204.     fclose(Names);
  205.     fclose(Index);
  206.     fclose(NewIndex);
  207.     FreeRemember(&RememberKey,TRUE);
  208.     CloseLibrary(IntuitionBase);
  209.     printf("Program completed normally.\n");
  210. }
  211.  
  212. void    Upcase(String)
  213. char *String;
  214.  
  215. {
  216.     do
  217.         if(*String >= 'a' && *String <= 'z')
  218.             *String  = _toupper(*String);
  219.     while(*++String != '\0');
  220. }
  221.  
  222. void    _abort()
  223. {
  224.     FreeRemember(&RememberKey,TRUE);
  225.     exit(FALSE);
  226. }
  227.